﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using GongXianDCUI.Models;

namespace GongXianDCUI.DAL
{
    public class LayoutArticleDAL
    {
        /// <summary>
        /// 查询菜单集合
        /// </summary>
        /// <returns></returns>
        public List<LayoutArticle> GetLayoutArticle()
        {
            DataTable lay = CommonLib.SQLHepler.GetDataTable("select *from LayoutArticle order by rname asc", null);
            //布局所有集合
            List<LayoutArticle> LayoutArticleList = new List<LayoutArticle>();
            foreach (DataRow item in lay.Rows)
            {
                LayoutArticle LayoutArticle = new LayoutArticle();
                LayoutArticle.Rname = item["Rname"] + "";
                LayoutArticle.RmaxCount = int.Parse(item["RmaxCount"] + "");
                LayoutArticle.RarticleCount = int.Parse(item["RarticleCount"] + "");
                LayoutArticle.Rbz = item["Rbz"] + "";
                //根据布局id查询子菜单
                DataTable funcitons = CommonLib.SQLHepler.GetDataTable("select top " + LayoutArticle.RmaxCount + " *from LayoutData a left join Funcitions b on b.FunctionId=a.RfunctionID where State=1 and Rname='" + LayoutArticle.Rname+"'", null);
                List<LayoutData> LayoutDataList = new List<LayoutData>();
                foreach (DataRow drFun  in funcitons.Rows)
                {
                    LayoutData LayoutData = new LayoutData();
                    Funcitions Funcitions = new Funcitions();
                    Funcitions.FunctionId =int.Parse( drFun["FunctionId"] + "");
                    Funcitions.FunctionName = drFun["FunctionName"] + "";
                    //根据子菜单查询文章列表
                    DataTable ArticleDT = CommonLib.SQLHepler.GetDataTable("select top "+ LayoutArticle.RarticleCount + " *from  Article where categoryId="+drFun["FunctionId"]+ " order by topFlag,hotFlag,pubdate desc", null);
                    List<Article> ArticleList = new List<Article>();
                    foreach (DataRow ArticleDR in ArticleDT.Rows)
                    {
                        Article Article = new Article();
                        Article.articleId = int.Parse(ArticleDR["articleId"] + "");
                        Article.title = ArticleDR["title"] + "";
                        Article.newFlag = ArticleDR["newFlag"] + "";
                        Article.hotFlag = ArticleDR["hotFlag"] + "";
                        Article.topFlag = ArticleDR["topFlag"] + "";
                        Article.picFlag = ArticleDR["picFlag"] + "";
                        Article.content = ArticleDR["content"] + "";
                        Article.ImageUrl= ArticleDR["ImageUrl"] + "";
                        Article.pubdate =DateTime.Parse( ArticleDR["pubdate"] + "");
                        ArticleList.Add(Article);
                    }
                    LayoutData.Functions = Funcitions;
                    LayoutData.ArticleList = ArticleList;
                    LayoutDataList.Add(LayoutData);
                }
                LayoutArticle.LayoutDataList = LayoutDataList;
                LayoutArticleList.Add(LayoutArticle);
            }
            return LayoutArticleList;
        }
        /// <summary>
        /// 获取电子公告默认菜单id为1000 
        /// </summary>
        /// <param name="funcitonID"></param>
        /// <returns></returns>
        public List<Article> getBulletin(int funcitonID)
        {
            return DB.Context.FromSql("select top 5 *from Article where categoryId=" + funcitonID + " order by topFlag,hotFlag,pubdate desc").ToList<Article>();
        }

    }
}
